//
//  LoginInputVM.swift
//  kxf
//
//  Created by Mac on 2024/3/4.
//

import Foundation
import UIKit

class LoginInputVM: UIView {
    
    let selfWidth = kFitWidth(260)
    let selfHeight = kFitWidth(48)
    
    var maxLength = 20

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(57), y: frame.origin.y, width: selfWidth, height: selfHeight))
        self.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(24)
        self.clipsToBounds = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var textField : UITextField = {
        let text = UITextField()
        text.keyboardType = .asciiCapable
        text.delegate = self
        
        return text
    }()
    lazy var getCodeBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("获取验证码", for: .normal)
        btn.setTitleColor(UIColor.THEME, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        
        return btn
    }()
}

extension LoginInputVM{
    func initUI() {
        addSubview(textField)
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(20))
            make.right.equalTo(kFitWidth(-20))
            make.centerY.lessThanOrEqualToSuperview()
        }
    }
    
    func showCodeBtn(){
        addSubview(getCodeBtn)
        getCodeBtn.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-20))
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(26))
        }
        textField.snp.remakeConstraints { make in
            make.left.equalTo(kFitWidth(20))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(getCodeBtn.snp.left).offset(kFitWidth(-20))
        }
    }
}


extension LoginInputVM:UITextFieldDelegate{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "" {
            return true
        }
        if textField.text?.count ?? 0 > self.maxLength-1{
            return false
        }
        return true
    }
}
